import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
          appBar: AppBar(
            title: const Text('文本输入框'),
          ),
          body: const MyHomePage()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _kw = "";
  search() {
    if (_kw.trim() != "") {
      print("输入了$_kw关键词");
    }
  }

  onKwChanged(value) {
    setState(() {
      _kw = (value as String).trim();
      print("一边输入一遍：");
      search();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('文本输入框_in'),
      ),
      body: Column(
        children: <Widget>[
          TextField(
            maxLength: 10,
            // maxLengthEnforcement: MaxLengthEnforcement.none,
            autofocus: true,
            onChanged: onKwChanged,
            onSubmitted: (value) {
              print("提交了：${value}");
              search();
            },
            textInputAction: TextInputAction.search,
            decoration: const InputDecoration(
              labelText: "关键词：",
              hintText: "请输入搜索关键词，多个关键词用空格分开",
              prefixIcon: Icon(Icons.search)
            ),
          ),
          ElevatedButton(onPressed: () {
            print("我要吃饭了");
            search();
          }, child: const Text("百度一下你就知道了"))
        ],
      ),
    );
  }
}
